<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;
use app\models\Admin;

class LoginController extends Controller
{
    function actionIndex(){
        $admin = new Admin();
        $request = Yii::$app->request;
        if (!$request->isPost) {
            return $this->render('login');
        } else {
            $username=$request->post('username', '');
            if($userinfo=$admin->findByUsername($username)){
                if($userinfo['password']==$request->post('password','')){
                    return json_encode(['status'=>1,'redirect'=>'http://www.baidu.com']);
                }else{
                    return json_encode(['status'=>0,'error'=>'密码错误']);
                }
            }else{
                return json_encode(['status'=>0,'error'=>'用户名错误']);
            }
        }
    }

    function actionTest()
    {

    }
}